package uit.comstore.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import uit.comstore.model.blo.BillBLO;
import uit.comstore.model.blo.BillDetailBLO;
import uit.comstore.model.blo.ProductBLO;
import uit.comstore.model.entity.Bill;
import uit.comstore.model.entity.BillDetail;
import uit.comstore.model.entity.Product;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BillAction extends ActionSupport {
	private Bill bill;
	private List<Bill> billList;
	private BillBLO billBLO;
	
	private BillDetailBLO billDetailBLO;
	private List<BillDetail> allBillDetailList;
	private BillDetail billDetail;
	
	private ProductBLO productBLO;
	private List<Product> productList;
	private Product product;
	
	private List<Product> idProductList;
	private List<BillDetail> selectedBillDetailList;
	
	private Long id;

	public BillAction() {
		productBLO = new ProductBLO();
		billBLO = new BillBLO();
		billDetailBLO = new BillDetailBLO();
	}

	public String execute() {
		this.productList = productBLO.inventoryProduct();
		this.allBillDetailList = billDetailBLO.list();
		this.billList = billBLO.list();
		return "success";
	}
	
	public String add(){
		try{
			Long total = 0L;
			Bill bill = getBill();
			bill.setDatePurchased(new Date());
			Set<BillDetail> billDetailSet = new HashSet<BillDetail>();
			for(int i=0;i< selectedBillDetailList.size();i++){
				BillDetail bd = selectedBillDetailList.get(i);
				Product product = productBLO.getById(idProductList.get(i).getId());
				
				bd.setProduct(product);
				bd.setBill(bill);
				bd.setProductPrice(bd.getProduct().getPrice());
				bd.setTotal(BigDecimal.valueOf(Long.valueOf(String.valueOf(bd.getProductPrice())) * bd.getProductQuantity()));
				total += Long.valueOf(String.valueOf(bd.getTotal()));
				billDetailSet.add(bd);
				
				/* Update number of product */
				product.setQuantity(product.getQuantity()-selectedBillDetailList.get(i).getProductQuantity());
				productBLO.edit(product);
			}
			bill.setTotal(BigDecimal.valueOf(total));
			bill.setBillDetail(billDetailSet);
			billBLO.add(bill);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return SUCCESS;
	}
	

	public Bill getBill() {
		return bill;
	}

	public void setBill(Bill bill) {
		this.bill = bill;
	}


	public List<Bill> getBillList() {
		return billList;
	}

	public void setBillList(List<Bill> billList) {
		this.billList = billList;
	}

	public BillBLO getBillBLO() {
		return billBLO;
	}

	public void setBillBLO(BillBLO billBLO) {
		this.billBLO = billBLO;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<BillDetail> getAllBillDetailList() {
		return allBillDetailList;
	}

	public void setAllBillDetailList(List<BillDetail> allBillDetailList) {
		this.allBillDetailList = allBillDetailList;
	}

	public BillDetail getBillDetail() {
		return billDetail;
	}

	public void setBillDetail(BillDetail billDetail) {
		this.billDetail = billDetail;
	}



	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public List<Product> getIdProductList() {
		return idProductList;
	}

	public void setIdProductList(List<Product> idProductList) {
		this.idProductList = idProductList;
	}

	public List<BillDetail> getSelectedBillDetailList() {
		return selectedBillDetailList;
	}

	public void setSelectedBillDetailList(List<BillDetail> selectedBillDetailList) {
		this.selectedBillDetailList = selectedBillDetailList;
	}
}
